<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="com.hddf.project.pojos.ProjectDoc"%>
<%@page import="com.hddf.project.pojos.ProjectTable"%>
<%@page import="com.hddf.project.service.ProjectService"%>
<%@page import="com.hddf.project.util.O"%>
<%@page import="java.io.OutputStream"%>
<%@page import="com.hddf.project.interceptor.CookieService"%>
<%@page import="com.hddf.project.interceptor.GetData"%>
<%@page import="java.io.File"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.io.FileInputStream"%>

<!DOCTYPE HTML PUBLIC "-W3CDTD HTML 4.01 TransitionalEN">
<html>
<head>
<title>下载</title>
<script type="text/javascript" src="/js/global_param.js"></script>
</head>
<%
	String name = "";
	String path = "";
	String fileName = "";
	CookieService cs = CookieService.getInstance();
	Cookie ctoken = cs.getCookieByName(request, "to");
	Cookie ctime = cs.getCookieByName(request, "ti");
	boolean b = (null != ctime && !"".equals(ctime.getValue()));
	boolean b2 = (null != ctoken && !"".equals(ctoken.getValue()));
	if (b && b2) {
		String web_token = ctoken.getValue();
		String web_time = ctime.getValue();

		String uri = request.getRequestURI();
		if (uri.indexOf("?") != -1) {
			uri = uri.substring(uri.lastIndexOf("/") + 1, uri.indexOf("?"));
		} else {
			uri = uri.substring(uri.lastIndexOf("/") + 1);
		}
		GetData gd = new GetData();
		Map<String, Object> m = gd.checkPermission(web_token, web_time, uri);

		String str = (String) m.get("msg");
		String permission = (String) m.get("permission");
		String state = (String) m.get("state");
		if (!O.ckSt(str) && state.equals("success")) {
			if (permission.equals("ok")) {
				Object _srcId = request.getParameter("srcId");
				Object _srcType = request.getParameter("srcType");
				if (null != _srcId && _srcType != null && O.ckNm(_srcId) && O.ckNm(_srcType)) {
					int srcId = Integer.valueOf(_srcId.toString());
					int srcType = Integer.valueOf(_srcType.toString());
					ProjectService ps = ProjectService.getInService();
					Map<String, String> map = ps.getDownloadDocument(srcId, srcType);
					if (null != map) {
		 				name = map.get("srcName");
						path = map.get("srcPath");

						fileName = new String(fileName.getBytes("GBK"), "iso8859-1");
						OutputStream os = response.getOutputStream(); //取得输出流
						response.reset(); //清空输出流
						response.setContentType("application/msexcel");
						response.setHeader("Content-disposition", "attachment; filename=" + name);

						File file = new File(path);
						boolean tof = file.exists();
						if (tof) {
							InputStream fis = new FileInputStream(path);// 获取文件
							byte[] block = new byte[1024];
							int length;
							while ((length = fis.read(block)) > 0) {
								os.write(block, 0, length);
							}
							fis.close();
						}
					}
				}
			} else {
%>
<script type="text/javascript">
	alert("您没有该操作的权限！");
	window.close();
</script>
<%
			}
		} else {
%>
<script type="text/javascript">
	alert(str);
</script>
<%
		}
	} else {
%>
<script type="text/javascript">
	alert("请您登录！");
	window.location.href = global_parameter.login_page;
</script>
<%
	}
%>
<body>
	<%=name%>
	<%=path%>
	<%=fileName%>
</body>
</html>